package org.batyrov.equery.action.admin;

import org.batyrov.equery.action.BaseAction;
import org.batyrov.equery.action.Const;
import org.batyrov.equery.bean.Service;
import org.batyrov.equery.bean.Window;

/**
 * Created by IntelliJ IDEA.
 * User: admin
 * Date: 25.07.2009
 * Time: 0:01:46
 * To change this template use File | Settings | File Templates.
 */
public class SaveWindow  extends BaseAction {
    private long id;
    private String name;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String execute() throws Exception {
        super.execute();
        if (name == null || name == "") {
            addActionError(getText("you_must_enter_name"));
            return Const.ERROR;
        }
        if (id == -1) {
            Window window = new Window();
            window.setName(name);
            if (dao.persist(window)) {
                return Const.SUCCESS;
            }
        } else {
            Window window = em.find(Window.class, id);
            if (window == null) {
                addActionError(getText("window_not_found"));
                return Const.ERROR;
            }
            em.getTransaction().begin();
            try {
                window.setName(name);
                em.getTransaction().commit();
                return Const.SUCCESS;
            } catch (Exception e) {
                e.printStackTrace();
                em.getTransaction().rollback();
                return Const.ERROR;
            }
        }
        return Const.SUCCESS;
    }

}
